﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ConsumeSys
{
    /// <summary>
    /// 静态方法类
    /// </summary>
    public static class StaticFun
    {
        /// <summary>
        /// md5加密，失败返回原始值
        /// </summary>
        /// <param name="obj">需要加密的对象</param>
        /// <returns>加密后的字符串</returns>
        public static string Jmd5(this string obj)
        {
            try
            { return Common.Globals.HashString(obj); }
            catch (Exception)
            { return obj; }
        }

        /// <summary>
        /// object转换为string，失败返回string.Empty
        /// </summary>
        /// <param name="obj">需要转换的对象</param>
        /// <returns>转换后的值</returns>
        public static string ToString2(this object obj)
        {
            string val = string.Empty;
            try
            { val = obj.ToString(); }
            catch (Exception)
            { val = string.Empty; }
            return val;
        }

        /// <summary>
        /// object转换为ToInt32，失败返回0
        /// </summary>
        /// <param name="obj">需要转换的对象</param>
        /// <returns>转换后的值</returns>
        public static int ToInt32(this object obj)
        {
            int val = 0;
            try
            { val = Convert.ToInt32(obj.ToString2().Split('.')[0]); }
            catch (Exception)
            { val = 0; }
            return val;
        }

        /// <summary>
        /// object转换为double，失败返回0
        /// </summary>
        /// <param name="obj">需要转换的对象</param>
        /// <returns>转换后的值</returns>
        public static double ToDouble(this object obj)
        {
            double val = 0;
            try
            { val = Convert.ToDouble(obj); }
            catch (Exception)
            { val = 0; }
            return val;
        }

        /// <summary>
        /// object转换为bool，失败返回false
        /// </summary>
        /// <param name="obj">需要转换的对象</param>
        /// <returns>转换后的值</returns>
        public static bool ToBoolean(this object obj)
        {
            bool val = false;
            try
            { val = Convert.ToBoolean(obj); }
            catch (Exception)
            { val = false; }
            return val;
        }

        /// <summary>
        /// object转换为DateTime，失败返回当前时间
        /// </summary>
        /// <param name="obj">需要转换的对象</param>
        /// <returns>转换后的值</returns>
        public static DateTime ToDateTime(this object obj)
        {
            DateTime val = DateTime.Now;
            try
            { val = Convert.ToDateTime(obj); }
            catch (Exception)
            { val = DateTime.Now; }
            return val;
        }

        /// <summary>
        /// object序列化为Json，失败返回null
        /// </summary>
        /// <param name="obj">需要转换的对象</param>
        /// <returns>Json格式数据</returns>
        public static string ToJson(this object obj)
        {
            try
            { return JsonConvert.SerializeObject(obj); }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// Json序列化为T
        /// </summary>
        /// <param name="obj">需要转换的Json</param>
        /// <returns>T</returns>
        public static T JsonToObject<T>(this string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }
    }
}
